<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../fragments/taglibs.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="decorator" content="default"/>
</head>
<body>
<div title="应急预案管理" style="padding:10px">
    <div id="tb" style="padding:3px">
        <span>预案名称:</span>
        <input class="easyui-searchbox" name="mem_name" data-options="prompt:'请输入预案名称',searcher:doSearch" style="width:200px">
    </div>
    <table id="dg"  class="easyui-datagrid" style="height:400px"
           url="${ctx}/aid_plan/pageList"
           toolbar="#toolbar" pagination="true"
           rownumbers="true" fitColumns="true" singleSelect="true" data-options="queryParams: {
                'pageSize' : function(){
                    return $('#dg').datagrid('getPager').pagination('options').pageSize;
                },
                'pageNumber' : function(){
                    return $('#dg').datagrid('getPager').pagination('options').pageNumber;
                }
            }">
        <thead>
        <tr>
            <th field="name" width="50">预案名称</th>
            <th field="applyScope" width="50">适配范围</th>
            <th field="levelName" width="50">事件级别</th>
            <th data-options="field:'_operate_2',width:50,formatter:lookProcess" >对应流程</th>
            <th data-options="field:'_operate_1',width:50,formatter:formatOper" >编辑流程</th>

        </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newAidPlan()">新建</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="edit()">编辑</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroy()">删除</a>
    </div>

    <div id="dlg" class="easyui-dialog" style="width:420px"
         closed="true" buttons="#dlg-buttons">
        <form id="fm" method="post" action="" novalidate style="margin:0;padding:20px 50px">
            <div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">定义预案基础信息</div>
            <input type="hidden" name="id">
            <input type="hidden" name="procdef_id">
            <div  style="margin-bottom:10px">
                <input name="name"  id="name" class="easyui-textbox" required="true" label="预案名称:" style="width:100%">
            </div>

            <div style="margin-bottom:10px">
                <select id="big_type" class="easyui-combobox" label="适配大类:" style="width:90%">
                    <c:forEach items="${dict}" var="item">
                        <c:if test="${item.CODE == 'eventBigType'}">
                            <c:forEach items="${dict}" var="big">
                                <c:if test="${item.ID == big.PARENT_ID}">
                                    <option value="${big.CODE}">${big.d_KEY}</option>
                                </c:if>
                            </c:forEach>
                        </c:if>
                    </c:forEach>
                </select>
            </div>
            <div id="nature" style="margin-bottom:10px">
                <select  id="nature_type" class="easyui-combobox" label="适配小类:" style="width:90%">
                    <c:forEach items="${dict}" var="item">
                        <c:if test="${item.CODE == 'nature'}">
                            <c:forEach items="${dict}" var="big">
                                <c:if test="${item.ID == big.PARENT_ID}">
                                    <option value="${big.d_KEY}">${big.d_KEY}</option>
                                </c:if>
                            </c:forEach>
                        </c:if>
                    </c:forEach>
                </select>
            </div>
            <div id="accident" style="margin-bottom:10px">
                <select  id="accident_type" class="easyui-combobox" label="适配小类:" style="width:90%">
                    <c:forEach items="${dict}" var="item">
                        <c:if test="${item.CODE == 'accident'}">
                            <c:forEach items="${dict}" var="big">
                                <c:if test="${item.ID == big.PARENT_ID}">
                                    <option value="${big.d_KEY}">${big.d_KEY}</option>
                                </c:if>
                            </c:forEach>
                        </c:if>
                    </c:forEach>
                </select>
            </div>
            <div id="health" style="margin-bottom:10px">
                <select  id="health_type" class="easyui-combobox" label="适配小类:" style="width:90%">
                    <c:forEach items="${dict}" var="item">
                        <c:if test="${item.CODE == 'health'}">
                            <c:forEach items="${dict}" var="big">
                                <c:if test="${item.ID == big.PARENT_ID}">
                                    <option value="${big.d_KEY}">${big.d_KEY}</option>
                                </c:if>
                            </c:forEach>
                        </c:if>
                    </c:forEach>
                </select>
            </div>
            <div id="society" style="margin-bottom:10px">
                <select  id="society_type" class="easyui-combobox" label="适配小类:" style="width:90%">
                    <c:forEach items="${dict}" var="item">
                        <c:if test="${item.CODE == 'society'}">
                            <c:forEach items="${dict}" var="big">
                                <c:if test="${item.ID == big.PARENT_ID}">
                                    <option value="${big.d_KEY}">${big.d_KEY}</option>
                                </c:if>
                            </c:forEach>
                        </c:if>
                    </c:forEach>
                </select>
            </div>

            <div style="margin-bottom:10px">
                <input name="applyScope" id="applyScope" class="easyui-textbox"  data-options="multiline:true" required="true" label="适用范围:" style="width:100%;height:60px">
            </div>
            <div style="margin-bottom:10px">
                <apan>事件级别:</apan>&nbsp;
                <c:forEach items="${lsFn:getDicts('eventLevel')}" var="item">
                    <input name="applyLevel"  type="radio" value="${item.ID}">${item.d_KEY}
                </c:forEach>
            </div>
            <div style="margin-bottom:10px">
                <select name="org_id"  class="easyui-combobox" label="对应应急机构:" style="width:90%">
                    <c:forEach items="${aidOrg}" var="org">
                        <option value="${org.id}">${org.name}</option>
                    </c:forEach>
                </select>
            </div>
        </form>
    </div>

    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveAidPlan()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
    </div>
    <script type="text/javascript">
        var url;
        function newAidPlan(){
            $('#dlg').dialog('open').dialog('center').dialog('setTitle','创建');
            $('#fm').form('clear');
            //document.getElementById("bigLevel").checked = true;//设置默认选中
        }
        function edit(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('center').dialog('setTitle','编辑');
                $('#fm').form('load',row);
            }
        }
        function saveAidPlan(){
            $('#fm').form('submit',{
                url:"${pageContext.request.contextPath}/aid_plan/edit",
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.status==1){
                        $.messager.show({
                            title: 'Error',
                            msg: result.msg
                        });
                    } else {
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the user data
                    }
                }
            });
        }
        function destroy(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $.messager.confirm('确认','确定删除吗？',function(r){
                    if (r){
                        $.post('${ctx}/aid_plan/delete',{id:row.id},function(result){
                            if (result.status==0){
                                $('#dg').datagrid('reload');    // reload the user data
                            } else {
                                $.messager.show({    // show error message
                                    title: '错误',
                                    msg: result.msg
                                });
                            }
                        },'json');
                    }
                });
            }
        }
        function doSearch(){
            $('#dg').datagrid('load',{
                queryName: $('input[name=mem_name]').val()
            });
        }
        $(function(){
            $("#accident").hide();
            $("#health").hide();
            $("#society").hide();
        })

        $("#big_type").combobox({
            onChange: function (newValue, oldValue) {
                if(newValue == "nature"){$("#nature").show();$("#accident").hide();$("#health").hide();$("#society").hide();}
                else  if(newValue == "accident"){$("#nature").hide();$("#accident").show();$("#health").hide();$("#society").hide();}
                else  if(newValue == "health"){$("#nature").hide();$("#accident").hide();$("#health").show();$("#society").hide();}
                else {$("#nature").hide();$("#accident").hide();$("#health").hide();$("#society").show();}
            }
        })

        $("#nature_type").combobox({
            onChange: function (newValue, oldValue) {setSmall(newValue);}
        })
        $("#accident_type").combobox({
            onChange: function (newValue, oldValue) {setSmall(newValue);}
        })
        $("#health_type").combobox({
            onChange: function (newValue, oldValue) {setSmall(newValue);}
        })
        $("#society_type").combobox({
            onChange: function (newValue, oldValue) {setSmall(newValue);}
        })

        $('#applyScope').textbox({
            onChange: function(value){
                setSmall();
            }
        });

        var array = new Array();
        var array_json = ${dict_json};
        for(var item in array_json){
            array.push(array_json[item].d_KEY);
        }
        function setSmall(typeName){
            var oldType = $("#applyScope").val()+","+typeName;
            var oldType_array = new Array();
            var newType="";
            if(oldType!=""){
                oldType_array = oldType.split(",");
                if(oldType_array.length>0){//去重同时去除不合理项
                    oldType_array = unique(oldType_array);//去重操作
                    oldType_array = deleteUnRational(oldType_array);//去除不合理数据项
                    for(var item in oldType_array){//还原成字符串
                        newType = newType+oldType_array[item];
                        if(item!=oldType_array.length-1){
                            newType = newType+",";
                        }
                    }
                }
            }
            if(newType!=null){
                typeName = newType;
                $("#applyScope").textbox("setValue",typeName);
            }else {
                $("#applyScope").textbox("setValue",typeName);
            }
        }
        /**
         * 数组去重方法
         * @param arr
         * @returns {Array}
         */
        function unique(ar) {
            var ret = [];
            for (var i = 0, j = ar.length; i < j; i++) {
                if (ret.indexOf(ar[i]) === -1) {
                    ret.push(ar[i]);
                }
            }
            return ret;
        }
        /**
         * 去除不合理数据项
         * @param arr
         */
        function deleteUnRational(arr){
            var result = [];
            for(var item in arr){
                if(contains(array,arr[item])){
                    result.push(arr[item]);
                }
            }
            return result;
        }

        /**
         * 查看 数据项是否在数组中存在
         * @param arr
         * @param obj
         * @returns {boolean}
         */
        function contains(arr, obj) {
            var i = arr.length;
            while (i--) {
                if (arr[i] === obj) {
                    return true;
                }
            }
            return false;
        }
        function formatOper(val,row,index){
            return '<a href="${ctx}/aid/procdef/update?planId='+row.id+'">编辑流程定义</a>';
        }
        function lookProcess(val,row,index){
            return '<a href="${ctx}/aid/procdef/update?planId='+row.id+'">查看处理流程</a>';
        }
    </script>
</div>
</body>
</html>
